<?php
/*
 * 车型库核心文件
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 7bd39d1564bb8e24336856bfadc86ee7b1499658 $
 */
defined('IN_IXDCW') or exit('Access Denied');

$arcid = isset($arcid) ? intval($arcid) : 0;
$armid = isset($armid) ? intval($armid) : 0;
$arbid = isset($arbid) ? intval($arbid) : 0;
if ($armid) {
	$arbid = AutoRank::getArbidByArmid($armid);
}
if ($arcid) {
	$armid = AutoRank::getArmidByArcid($arcid);
	$arbid = AutoRank::getArbidByArcid($arcid);
}
$exturl = '';
$exturl .= (isset($arbid) && !empty($arbid)) ? "&arbid=$arbid" : '';
$exturl .= (isset($arcid) && !empty($arcid)) ? "&arcid=$arcid" : '';
$exturl .= (isset($armid) && !empty($armid)) ? "&armid=$armid" : '';

if ($arcid) {
	$carInfo = AutoCar::get($arcid);
}
if ($arbid) {
	$brandInfo = AutoBrand::get($arbid);
}
if ($armid) {
	$manuInfo = AutoManufactor::get($armid);
}
$menus = array(
	array('添加商用车', '?moduleid=' . $moduleid . '&action=add&modelType=commercial' . $exturl),
	array('添加乘用车', '?moduleid=' . $moduleid . '&action=add&modelType=passenger' . $exturl),
	array('添加房车', '?moduleid=' . $moduleid . '&action=add&modelType=rv' . $exturl),
	array('视频', '?moduleid=10&' . $exturl),
	array('回收站', '?moduleid=' . $moduleid . '&action=recycle' . $exturl),
);
if (!isset($modelType)) $modelType = AutoModel::MODEL_PASSENGER;

$do = new AutoModelModule($modelType);

if (in_array($action, array('add', 'edit'))) {
	$MFD = $do->getMainFields();
	$EFD = $do->getPropGroup();
}

if (in_array($action, array('', 'check', 'reject', 'recycle'))) {
	$sfields = array('车款名称');
	$dfields = array('name');
	$sorder  = array('添加时间降序', '添加时间升序', '更新时间降序', '更新时间升序', '价格升序排列', '价格降序排列');
	$dorder  = array('addtime DESC', 'addtime ASC', 'edittime DESC', 'edittime ASC', 'price ASC', 'price DESC');

	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;
	$level = isset($level) ? intval($level) : 0;

	isset($datetype) && in_array($datetype, array('edittime', 'addtime')) or $datetype = 'addtime';
	$fromdate = isset($fromdate) && preg_match("/^([0-9]{8})$/", $fromdate) ? $fromdate : '';
	$fromtime = $fromdate ? strtotime($fromdate . ' 0:0:0') : 0;
	$todate   = isset($todate) && preg_match("/^([0-9]{8})$/", $todate) ? $todate : '';
	$totime   = $todate ? strtotime($todate . ' 23:59:59') : 0;
	$pagesize = isset($psize) && $psize > 0 ? intval($psize) : 20;
	$thumb    = isset($thumb) ? intval($thumb) : 0;
	$link     = isset($link) ? intval($link) : 0;
	$year     = isset($year) ? intval($year) : 0;

	$fields_select = dselect($sfields, 'fields', '', $fields);
	$level_select  = level_select('level', '推荐等级', $level);
	$order_select  = dselect($sorder, 'order', '', $order);

	$condition = ' 1 ';
	$condition .= (!empty($arbid)) ? " and arbid=$arbid" : '';
	$condition .= (!empty($arcid)) ? " and arcid=$arcid" : '';
	$condition .= (!empty($armid)) ? " and armid=$armid" : '';

	if ($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	if ($level) $condition .= " AND level=$level";
	if ($fromtime) $condition .= " AND `$datetype`>=$fromtime";
	if ($totime) $condition .= " AND `$datetype`<=$totime";
	if ($thumb) $condition .= " AND thumb<>''";
	if ($guest) $condition .= " AND username=''";
	if ($year) $condition .= " AND year={$year}";

	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';
	$do->setPagesize($pagesize);
}
switch ($action) {
	case 'grouporder':
		$type = $type ? $type : 0;
		$aid or msg();
		$condition = " t.aid='$aid' ";
		if ($type) {
			$condition .= " AND type='$type'";
		}
		$GroupOrder = new AutoGroupOrder();
		$lists      = $GroupOrder->getList(" $condition");
		include tpl('grouporder', $module);
		break;
	case 'add':
		if ($submit) {
			if ($do->pass($post)) {
				$do->add($post, $post_fields);
				dmsg('添加成功', Env::referer());
			} else {
				msg($do->getErrmsg());
			}
		} else {
			$tname = $menus[$menuid][0];
			include tpl('edit', $module);
		}
		break;
	case 'edit':
		$amid or msg();

		$do->setAmid($amid);
		$item = $do->getOne();
		if ($submit) {
			if ($do->pass($post)) {
				$do->edit($post, $post_fields);
				dmsg('修改成功', Env::referer());
			} else {
				msg($do->getErrmsg());
			}
		} else {
			extract($item);
			include tpl('edit', $module);
		}
		break;
	case 'updateCategory';
		$amids or msg('请选择需要批量更新的车款');
		$do->updateCategory($amids, $arbid, $armid, $arcid);
		dmsg('更新车款分类成功', Env::referer());
		break;
	case 'updateModelname':
		$uAmid      = isset($uAmid) ? intval($uAmid) : 0;
		$uModelName = isset($uModelName) ? intval($uModelName) : 0;

		$uAmid or dmsg('传值错误');
		$uModelName or dmsg('传值错误');
		if ($do->updateModelName($uAmid, $uModelName)) {
			echo '修改价格为' . $uModelName . '成功';
		}

		break;
	case 'updateModelPrice':
		$uAmid       = isset($uAmid) ? intval($uAmid) : 0;
		$uModelPrice = isset($uModelPrice) ? intval($uModelPrice) : 0;

		$uAmid or dmsg('传值错误');

		if ($do->updateModelPrice($uAmid, $uModelPrice)) {
			echo '修改价格为' . $uModelPrice . '成功';
		}
		break;
	case 'delete':
		$amid or msg('请选择车款');
		$do->delete($amid);
		dmsg('删除成功', Env::referer());
		break;
	case 'restore':
		$itemid or msg('请选择' . $MOD['name']);
		$do->restore($itemid);
		dmsg('还原成功', $forward);
		break;
	case 'clear':
		$amid or msg('请选择车款');
		$do->clear($amid);
		dmsg('清空成功', $forward);
		break;
	case 'level':
		$itemid or msg('请选择' . $MOD['name']);
		$level = intval($level);
		$do->level($itemid, $level);
		dmsg('级别设置成功', $forward);
		break;
	case 'recycle':
		$condition .= ' and status=0';
		$lists  = $do->getList($condition);
		$menuid = 3;
		include tpl('index', $module);
		break;
	case 'reject':
		if ($itemid && !$psize) {
			$do->reject($itemid);
			dmsg('拒绝成功', $forward);
		} else {
			$lists  = $do->get_list('status=1' . $condition, $dorder[$order]);
			$menuid = 3;
			include tpl('index', $module);
		}
		break;
	case 'check':
		if ($itemid && !$psize) {
			$do->check($itemid);
			dmsg('审核成功', $forward);
		} else {
			$lists  = $do->get_list('status=2' . $condition, $dorder[$order]);
			$menuid = 2;
			include tpl('index', $module);
		}
		break;
	case 'from':
		$condition = "status=3";
		if ($keyword) $condition .= " AND (`copyfrom` LIKE '%$keyword%' OR `fromurl` LIKE '%$keyword%')";
		$lists  = array();
		$result = $db->query("SELECT COUNT(`copyfrom`) AS num,copyfrom,fromurl FROM {$table} WHERE $condition GROUP BY `copyfrom` ORDER BY num DESC LIMIT 0,50");
		while ($r = $db->fetchArray($result)) {
			if (!$r['copyfrom']) continue;
			$lists[] = $r;
		}
		include tpl('from', $module);
		break;
	default:
		$condition .= ' and status!=0';
		$lists = $do->getList($condition, $dorder[$order]);
		include tpl('index', $module);
		break;
}